using System;
using System.Collections.Generic;
using System.Text;
using SwinMessengerLibrary;

namespace SwinMessengerConsoleApp
{
    class Messenger
    {
        private Server _Server;
        private CommandProcessor processor = new CommandProcessor();
        private string _CurrentMessage;
        /// <summary>
        /// This allows the user to log into the server and execute all of the commands in the command processor
        /// </summary>
        public void Execute()
        {
            _Server = GetInformation();
            while (_Server.CanLogin)
            {
                System.Console.WriteLine("The information you enered was wrong!");
                _Server = GetInformation();
            }
            Console.WriteLine("Getting roster");
            for (int i = 0; i < 100; i++)
            {
                Console.Write(".");
                System.Threading.Thread.Sleep(50);
            }
            System.Console.Clear();
            MessageRecivedEvent();
            Console.WriteLine("Type 'help' for help");
            bool quit = false;
            
            while( quit == false)
            {

                ReadCommand();
                List<string> tempList = new List<string>();
                tempList = processor.Execute(_Server, _CurrentMessage);

                foreach (string temp in tempList)
                {
                    Console.WriteLine(temp);
                }
                if (_CurrentMessage.ToLower() == "quit" || _CurrentMessage.ToLower() == "logout")
                {
                    quit = true;
                }
            }
        }

        private void ReadCommand()
        {
            char ch;
            _CurrentMessage = "";
            do
            {
                ch = Console.ReadKey(true).KeyChar;
                if (ch == (Char)8 && _CurrentMessage.Length > 0)
                {
                    Console.CursorLeft = Console.CursorLeft - 1;
                    Console.Write(" ");
                    Console.CursorLeft = Console.CursorLeft - 1;
                    _CurrentMessage = _CurrentMessage.Substring(0, _CurrentMessage.Length - 1);
                }
                else
                {
                    if (ch > (Char)31)
                    {
                        Console.Write(ch);
                        _CurrentMessage = _CurrentMessage + ch;
                    }
                }

            } while (ch != (Char)13);
            System.Console.WriteLine("");
        }
        private void MessageRecivedEvent()
        {
            foreach (Contact con in _Server.Roster.Contacts)
            {
                con.OnMessage += new EventHandler(Contact_OnMessage);
            }
        }
        private void Contact_OnMessage(object sender, EventArgs e)
        {
            Console.CursorLeft = 0;
            if (e is MessageEventArgs && e != null)
            {
                if (((MessageEventArgs)e).Message == null)
                {
                    if (((MessageEventArgs)e).State.ToLower() == "composing")
                    {
                        System.Console.WriteLine(((MessageEventArgs)e).Id + " is writing");
                    }
                    if (((MessageEventArgs)e).State.ToLower() == "paused")
                    {
                        System.Console.WriteLine(((MessageEventArgs)e).Id + " has stoped typing");
                    }
                    if (((MessageEventArgs)e).State.ToLower() == "active")
                    {
                        System.Console.WriteLine(((MessageEventArgs)e).Id + " is not writing");
                    }
                }
                else
                {
                    System.Console.WriteLine("Message from: "+((MessageEventArgs)e).Id);
                    System.Console.WriteLine("- "+((MessageEventArgs)e).Message);
                }
            }
            Console.Write(_CurrentMessage);
        }
        private Server GetInformation()
        {
            Server server;
            System.Console.WriteLine("Enter the following fields");
            System.Console.Write("Server name:");
            string serverID = System.Console.ReadLine();
            System.Console.Write("Account name:");
            string account = System.Console.ReadLine();
            System.Console.Write("Password:");
            string password = PasswordHider();
            server = new Server(serverID, account, password);

            server.Login();
            for (int i = 0; i < 300; i++)
            {
                if (server.CanLogin)
                {
                    Console.Write(".");
                    System.Threading.Thread.Sleep(50);
                }
                else
                {
                    break;
                }
            }
            System.Console.Clear();

            
            return server;
        }
        private string PasswordHider()
        {
            char ch;
            string word = "";
            do
            {
                ch = Console.ReadKey(true).KeyChar;
                if (ch == (Char)8 && word.Length > 0)
                {
                    Console.CursorLeft = Console.CursorLeft - 1;
                    Console.Write(" ");
                    Console.CursorLeft = Console.CursorLeft - 1;
                    word = word.Substring(0, word.Length - 1);
                }
                else
                {
                    if (ch > (Char)31)
                    {
                        Console.Write("*");
                        word = word + ch;
                    }
                }
                    
            } while (ch != (Char)13);
            System.Console.WriteLine(""); //to put the next text on a new line
            return word;
        }
    }
}
